package m3_publishsubscribe;

import com.rabbitmq.client.*;

import java.io.IOException;

public class Consumer {
    public static void main(String[] args) throws Exception {
        ConnectionFactory f = new ConnectionFactory();
        f.setHost("192.168.126.129");
        f.setUsername("admin");
        f.setPassword("admin");

        Channel channel = f.newConnection().createChannel();
        //1.定义交换机
        channel.exchangeDeclare("logs", BuiltinExchangeType.FANOUT);
        //2.定义随机名称队列
        //String queue = UUID.randomUUID().toString();
        //channel.queueDeclare(queue,false,true,true,null);
        String queue = channel.queueDeclare().getQueue();
        //3.绑定到交换机
        channel.queueBind(queue,"logs","");
        //4.正常消费数据
        DeliverCallback deliverCallback = new DeliverCallback() {
            @Override
            public void handle(String consumerTag, Delivery message) throws IOException {
                String msg = new String(message.getBody());
                System.out.println("收到:" + msg);
            }
        };
        CancelCallback cancelCallback=new CancelCallback() {
            @Override
            public void handle(String consumerTag) throws IOException {
            }
        };

        channel.basicConsume(queue,true, deliverCallback, cancelCallback);

    }
}
